<template>
  <div class="search-wrapper">
    <nav-header></nav-header>
    <search-box @change="change"></search-box>
    <div v-if="data" class="pop_search">
      <div v-if="data.length==0" class="no_search_result">没有找到相关股票</div>
      <ul v-else class="search_list">
        <li @click="jumpIntelligentDiagnosis(item)" :key="index" v-for="(item,index) in data" class="bdb">
          <div class="s_secname">{{item.sCHNShortName}}</div>
          <div class="s_seccode">{{item.sDtSecCode}}</div>
        </li>
      </ul>
    </div>
    <div v-if="dataHot" class="hot_search_block">
      <div class="hot_search_title">
        <strong>热门搜索</strong>
        <span @click="changeBatch" id="changeBatch" class>换一批</span>
      </div>
      <div v-if="dataHot.length==0" class="no_data none">暂无热门搜索股票</div>
      <ul v-else class="hot_search_list">
        <li @click="jumpIntelligentDiagnosis(item1)" :key="index" v-for="(item1,index) in dataHot">{{item1.sSecName}}</li>
      </ul>
    </div>

    <div
      class="risk_note"
    >本行情工具由灯塔财经信息有限公司提供，内容基于市场公开信息的大数据分析，非川财证券及灯塔财经的投资建议或收益保证，不会降低投资品的各项风险，请自主决定，谨慎投资。</div>
  </div>
</template>
<script>
import NavHeader from "@/components/header";
import SearchBox from "@/components/search";
import { search, getHot } from "../../api/intelligentDiagnosis";
import { getArrayItems } from "../../utils/utils";

export default {
  components: {
    NavHeader,
    SearchBox
  },
  data() {
    return {
      data: null,
      dataHot: null
    };
  },
  methods: {
    jumpIntelligentDiagnosis(item){
         this.$router.push({
          path: '/intelligentDiagnosis/intelligentDiagnosis',
          query: {
            sDtSecCode: item.sDtSecCode,
            sSecName: item.sSecName
          }
        })
    },
    changeBatch() {
      let temp = [...this.hot];
      this.dataHot = getArrayItems(temp, 8);
    },
    startListen(key) {
      if (this.listenTimer != null) {
        clearTimeout(this.listenTimer);
      }
      this.listenTimer = setTimeout(() => {
        if (key == "") {
          this.data = null;
          console.log(this.data);
          return;
        } else {
          this.ajaxSearch(key);
        }
      }, 400);
    },
    change(key) {
      this.startListen(key);
    },
    async ajaxSearch(key) {
      let data = await search(key);
      this.data = data.vStockItem;
    },
    async getHot() {
      let dataHot = await getHot();
      this.hot = dataHot.vList;
      this.changeBatch();
    }
  },
  created() {
    this.hot = [];
    this.listenTimer = null;
  },
  mounted() {
    this.getHot();
  }
};
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
@import "../../assets/styles/global";

.search-wrapper {
  width: 100%;
  height: 100%;
  background: white;
  .risk_note {
      padding: 10px 15px;
      font-size: 12px;
      color: #ababab;
      position: absolute;
      bottom: 10px;
    }
  .pop_search {
    width: 100%;
    height: auto;
    z-index: 100;
    background: #fff;
    .no_search_result {
      text-align: center;
      font-size: px2rem(14);
      padding: px2rem(25) 0 0;
      color: rgba(37, 37, 37, 0.6);
      line-height: 1.7;
    }
    .search_list {
      .bdb {
        position: relative;
        padding: 10px 15px;
        border-bottom: 1px #e4e4e4 solid;
        .s_secname {
          font-size: 16px;
        }
        .s_seccode {
          font-size: 12px;
          color: rgba(37, 37, 37, 0.6);
        }
      }
    }
  }
  .hot_search_block {
    padding: 16px 15px 5px;
    background-color: #fff;
    margin-bottom: 8px;
    .hot_search_title {
      width: 100%;
      margin-bottom: 15px;
      position: relative;
      border-bottom: 0.5px #e4e4e4 solid;
      height: 36px;
      span {
        display: block;
        font-size: 12px;
        position: absolute;
        padding: 5px 10px;
        right: 0;
        top: 0;
        border: 1px solid #e5e5e5;
        border-radius: 20px;
      }
      strong {
        font-size: 18px;
        vertical-align: top;
        font-weight: bold;
      }
    }
    .no_data {
      text-align: center;
      padding: 40px 0;
      font-size: 14px;
      color: rgba(37, 37, 37, 0.6);
    }
    .hot_search_list {
      margin: 0 -2%;
      overflow: hidden;
      color: #999999;
      padding-top: 2px;
      li {
        float: left;
        width: 21%;
        margin: 0 2% 10px;
        font-size: 14px;
        text-align: center;
        padding: 4px 0;
        box-sizing: border-box;
        color: #999999;
        -webkit-box-sizing: border-box;
        height: 36px;
        line-height: 30px;
        background: #f4f6f5;
        border-radius: 6px;
      }
    }
 
  }
}
</style>
